from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
from flask_cors import CORS

db = SQLAlchemy()

def create_app(config_name='default'):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    
    db.init_app(app)
    
    # 注册所有接口蓝图
    from app.api import register_blueprints
    register_blueprints(app)
    
    # 为所有路由配置 CORS
    CORS(app, 
         resources={r"/dev-api/*": {
             "origins": "*",
             "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
             "allow_headers": "*"
         }},
         supports_credentials=True)
    
    return app